package main

import (
	"encoding/xml"
	"fmt"
	"html/template"
	"io"
	"log"
	"os"
	"os/exec"
	"strconv"
	"test/config"
	"test/unit_function/compile_and_download"
	"test/unit_function/email"
	"test/unit_function/file"
	"test/unit_function/flash_device"
	"test/unit_function/result"
	"test/unit_function/sshdata"
	"test/unit_function/syncOH"
	"test/unit_function/unique_snowflake"
	"time"
	"unsafe"
)

const (
	OpenHarmony40Beta1 = "OpenHarmony-4.0-Beta1"
	Default            = "default"
	OpenHarmony40Beta2 = "OpenHarmony-4.0-Beta2"
)

func Init() {
	// 初始化
	config.LogInit()
	unique_snowflake.Init()
}

func PostProcess() {
	// 后置处理，将log/test.log重命名为日期+时间
	os.Rename("log/test.log", "log/test_"+time.Now().Format("2006-01-02 15:04:05 MST Mon")+".log")
}

func TestHtml() {
	templateText := `
<!DOCTYPE html>
<html>
<head>
    <title>Template Example</title>
</head>
<body>
    <h1>HTML Template Example</h1>
    {{.RawHTML}}
</body>
</html>
`

	// 准备数据
	data := struct {
		RawHTML template.HTML
	}{
		RawHTML: template.HTML(`<p>This is <strong>raw</strong> HTML code.</p>`),
	}

	// 解析模板
	tmpl, err := template.New("example").Parse(templateText)
	if err != nil {
		panic(err)
	}

	// 渲染模板
	err = tmpl.Execute(os.Stdout, data)
	if err != nil {
		panic(err)
	}
}

func GenerateResultHtml(entity result.ResultEntity) string {
	file, err := os.Create("html/result.html")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	err = file.Truncate(0)
	if err != nil {
		log.Fatal(err)
	}
	tmpl, err := template.ParseFiles("html/template.html")
	if err != nil {
		log.Fatal(err)
	}
	err = tmpl.Execute(file, entity)
	if err != nil {
		log.Fatal(err)
	}

	open, err := os.Open("html/result.html")
	if err != nil {
		log.Fatal(err)
	}
	defer open.Close()
	all, err := io.ReadAll(open)
	if err != nil {
		log.Fatal(err)
	}
	return string(all)

}

type Recurlyservers struct {
	XMLName     xml.Name `xml:"servers"`
	Version     string   `xml:"version,attr"`
	Svs         []server `xml:"server"`
	Description string   `xml:",innerxml"`
}

type server struct {
	XMLName    xml.Name `xml:"server"`
	ServerName string   `xml:"serverName"`
	ServerIP   string   `xml:"serverIP"`
}

func TestMergeResult() {
	entity := result.GetResult(result.Benchmark{
		MuslBenchmarkFilePath:   "D:/auto_test/benchmark/result/musl_compare_result",
		BionicBenchmarkFilePath: "D:/auto_test/benchmark/result/musl_compare_result",
		XtsFilePath:             "D:/xts_suite/suites_tmp/acts/reports/2023-06-05-19-56-13",
	})
	GenerateResultHtml(entity)
}

func TestFlash() {
	flash_device.FlashDevice("D:/DeployDevice/resource/RK3568_tool/upgrade_tool.exe", "D:/downloads/version-Daily_Version-dayu200-20230609_070104-dayu200_img")
}

func main() {
	flag := make(chan string)
	// 初始化
	Init()

	//测试编译镜像功能
	arr := make([]compile_and_download.BuildTarget, 1)
	target := compile_and_download.Rk3568ImgFromInternet{}
	go func() {
		err := target.Compile()
		if err != nil {
			log.Fatal(err)
		}
		err = target.Download()
		if err != nil {
			log.Fatal(err)
		}
		err = target.Execute()
		if err != nil {
			log.Fatal(err)
		}
		target := (*compile_and_download.Rk3568Img)(unsafe.Pointer(&target))
		flag <- target.ImgLink
		//flag <- "asd"
	}()
	//拉最新的代码
	version := Default
	err := syncOH.SyncCode(version, sshdata.GetConnection())
	if err != nil {
		log.Fatal(err)
	}
	var link string
	link = <-flag
	//link = "from Img"
	fmt.Println("link=", link)
	//arr[0] = &compile_and_download.Rk3568Img{}
	arr[0] = &compile_and_download.Rk3568Xts{}
	//arr[1] = &compile_and_download.RK3568Libc{}
	//arr[1] = &compile_and_download.Rk3568Benchmark{}
	for i := range arr {
		err := arr[i].Compile()
		if err != nil {
			log.Fatal(err)
		}
		err = arr[i].Download()
		if err != nil {
			log.Fatal(err)
		}
		err = arr[i].Execute()
		if err != nil {
			log.Fatal(err)
		}
	}
	if version == Default {
		version = "master"
	}
	// MergeResult
	entity := result.GetResult(result.Benchmark{
		Version:                 version,
		DownloadLink:            link,
		MuslBenchmarkFilePath:   "D:/auto_test/benchmark/result/musl_compare_result",
		BionicBenchmarkFilePath: "D:/auto_test/benchmark/result/bionic_compare_result",
		XtsFilePath:             "D:/auto_test/result/xtsLatest",
	})
	all := GenerateResultHtml(entity)
	all1 := result.GetHtmlResult()
	all2 := result.GetLineChart()
	// 将文件放入result文件夹下面
	_, err = file.Copy("html/result.html", "D:/auto_test/result/result.html")
	if err != nil {
		log.Fatal(err)
	}
	// 打包全部结果，并发送邮件
	now := time.Now()
	fileName := strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "-" + strconv.Itoa(now.Day()) + "_result.tar.gz"
	cmd := exec.Command("tar", "-zcvf", fileName, "result")
	cmd.Dir = "D:/auto_test"
	err = cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	// 生成唯一的测试id
	//benchmark.GenerateBenchmarkDatabaseData(unique_snowflake.Generate())
	err = email.SendEmail("D:/auto_test/"+fileName, fileName, all+all1+all2)
	if err != nil {
		log.Fatal(err)
	}

}
